package com.noble.glamour.module.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;

import com.noble.glamour.bean.config.base.Action;
import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.WebActivity;
import com.noble.glamour.module.logic.config.FinalKey;
import com.noble.glamour.ui.main.MainActivity;
import com.noble.glamour.ui.push.WebPushDialog;
import com.noble.glamour.ui.setting.BottomBannedDialog;
import com.noble.glamour.ui.user.check.edit.EditContentAct;
import com.noble.glamour.ui.user.check.other.UserBlockAct;
import com.noble.glamour.ui.user.my.MyDiamondsExplainAct;
import com.noble.glamour.ui.user.my.gift.MyGiftActivity;
import com.noble.glamour.ui.user.update.UpdateDialog;
import com.noble.glamour.ui.web.util.WebUtil;
import com.noble.library.host.Hosts;
import com.noble.library.log.PToast;
import com.noble.meether.R;
import com.noble.meether.chat.ui.agora.act.dialog.GoodsDiamondBottomDlg;
import com.noble.meether.chat.ui.imui.IMActivity;
import com.noble.meether.logic.center.EditKey;
import com.noble.meether.logic.common.bean.Updata;
import com.noble.meether.mail.ui.friend.MyDefriendAct;
import com.noble.meether.notify.act.LockScreenActivity;
import com.noble.meether.pay.PayRefreshDlg;
import com.noble.meether.pay.PayResultDlg;
import com.noble.meether.ui.reg.info.RegInfoAct;
import com.noble.meether.ui.reg.nav.NavUserAct;
import com.noble.meether.ui.wallet.WithDrawExplainAct;

import java.util.Map;

/**
 * 应用内页面跳转工具
 */
public class UIShow {
    // ---------------------------应用内弹出及跳转------------------------------

    public static void show(Context context, Intent intent) {
        context.startActivity(intent);
    }

    public static void show(Context context, Class clz, int flag) {
        Intent intent = new Intent(context, clz);
        if (flag != -1) intent.addFlags(flag);
        show(context, intent);
    }

    public static void show(Context context, Class clz) {
        show(context, clz, -1);
    }

    /**
     * 显示activity并清空栈里其他activity
     *
     * @param activity 要启动的activity
     */
    public static void showActivityClearTask(Context context, Class activity) {
        show(context, activity, Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    }

    /**
     * 调用改方法启动activity，可将应用从后台唤起到前台，
     */
    public static void showActivityFromBack(Class activity) {
        show(App.context, activity, Intent.FLAG_ACTIVITY_NEW_TASK);
    }

    /**
     * 跳转到主页并清除栈里的其他页面
     */
    public static void showMainClearTask(Context context) {
        showActivityClearTask(context, MainActivity.class);
    }

    /**
     * 模拟软件退出[携带特殊的intent参数跳转到首页]
     *
     * @param context 上下文
     */
    public static void simulateExit(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra(FinalKey.SIMULATE_EXIT_KEY, FinalKey.SIMULATE_EXIT_KEY);
        show(context, intent);
    }

    /**
     * 跳转到首页的指定tab，并传值，通过MainActivity的OnNewIntent接收
     *
     * @param tabType 指定的跳转tab
     * @param dataMap 跳转时通过intent传递的map值
     */
    public static void showMainWithTabData(Context context, int tabType, Map<String, String> dataMap) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra(FinalKey.HOME_TAB_TYPE, tabType);
        if (dataMap != null)
            for (Map.Entry<String, String> entry : dataMap.entrySet()) {
                intent.putExtra(entry.getKey(), entry.getValue());
            }
        context.startActivity(intent);
    }

    /**
     * 应用外消息提示点击跳转到信箱fragment
     *
     * @param context 上下文
     */
    public static void showMainWithBackMessage(Context context) {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mainIntent.setClass(context, MainActivity.class);
        mainIntent.putExtra(FinalKey.HOME_TAB_TYPE, FinalKey.MAIN_TAB_2);//消息列表
        context.startActivity(mainIntent);
    }

    /**
     * 跳转到网页
     *
     * @param type 1-侧滑页面，2-全屏页面，全屏时显示loading条
     * @param url  网页地址
     */
    public static void showWebActivity(Context context, int type, String url) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("url", url);
        intent.putExtra("type", type);
        show(context, intent);
    }

    /**
     * 跳转到网页
     *
     * @param url 网页地址
     */
    public static void showWebActivity(Context context, String url) {
        showWebActivity(context, 1, url);
    }

    /**
     * 跳转到系统设置面面
     */
    public static void showNetworkSettings(Context context) {
        Intent intent;
        //判断手机系统的版本  即API大于10 就是3.0或以上版本
        if (android.os.Build.VERSION.SDK_INT > 10) {
            intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else {
            intent = new Intent();
            ComponentName component = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
            intent.setComponent(component);
            intent.setAction("android.intent.action.VIEW");
        }
        context.startActivity(intent);
    }

    /**
     * 打开导航页
     */
    public static void showNavUserAct(FragmentActivity activity) {
        showActivityClearTask(activity, NavUserAct.class);
    }

    /**
     * 打开注册资料页
     */
    public static void showRegInfoAct(FragmentActivity activity) {
        Intent intent = new Intent(activity, RegInfoAct.class);
        activity.startActivity(intent);
    }

    //============================== 小友模块相关跳转 =============================

    /**
     * 打开关于页面
     */
    public static void showAboutAct(Context context) {
        showWebActivity(context, WebUtil.jointUrl(Hosts.H5_APP_ABOUT));
    }

    /**
     * 打开主播管理规范
     */
    public static void showAnchorManage(Context context) {
        showWebActivity(context, WebUtil.jointUrl(Hosts.H5_APP_ANCHOR_MGR));
    }

    /**
     * 打开平台规范
     */
    public static void showPlatformSpecific(Context context) {
        showWebActivity(context, WebUtil.jointUrl(Hosts.H5_APP_PLATFORN_MGR));
    }

    /**
     * 打开隐私政策
     */
    public static void showPrivacyPolicy(Context context) {
        showWebActivity(context, WebUtil.jointUrl(Hosts.H5_APP_PRIVACY_POLICY));
    }

    /**
     * 打开用户注册协议
     */
    public static void showRegisterAgree(Context context) {
        showWebActivity(context, WebUtil.jointUrl(Hosts.H5_APP_REGISTER_AGREE));
    }

    /**
     * 打开交友注意事项
     */
    public static void showAppTips(Context context) {
        showWebActivity(context, WebUtil.jointUrl(Hosts.H5_APP_TIPS));
    }

    /**
     * 打开用户账号封禁页
     */
    public static void showUserBlockAct(Context context) {
        context.startActivity(new Intent(context, UserBlockAct.class));
    }

    /**
     * 打开编辑昵称页
     */
    public static void showEditContentAct(FragmentActivity context, String defaultValue, int editType) {
        Intent intent = new Intent(context, EditContentAct.class);
        intent.putExtra("intentType", editType);
        intent.putExtra("defaultValue", defaultValue);
        context.startActivityForResult(intent, EditKey.EDIT_REQUEST_CODE);
    }

    /**
     * 打开锁屏弹窗弹出的activity
     */
    public static void showLockScreenActivity() {
        Intent intent = new Intent(App.context, LockScreenActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        App.context.startActivity(intent);
    }

    /**
     * 软件升级逻辑处理
     *
     * @param activity  FragmentActivity上下文
     * @param appUpdate 软件升级信息
     */
    public static void showUpdateDialog(final FragmentActivity activity, final Updata appUpdate) {
        UpdateDialog updateDialog = new UpdateDialog();
        updateDialog.setData(appUpdate);
        updateDialog.showDialog(activity);
    }

    /**
     * 打开私信聊天内容页
     *
     * @param mContext  上下文
     * @param whisperID 私聊ID
     * @param name      名称（可有可无）
     */
    public static void showPrivateChatAct(Context mContext, long whisperID, String name, String avatar, boolean isVip) {
        Intent intent = new Intent(mContext, IMActivity.class);
        intent.putExtra(IMActivity.WHISPER_ID, whisperID);
        intent.putExtra(IMActivity.WHISPER_NAME, name);
        intent.putExtra(IMActivity.WHISPER_AVATAR, avatar);
        intent.putExtra(IMActivity.WHISPER_VIP, isVip);
        mContext.startActivity(intent);
    }

    /**
     * 通过配置调起的web-dialog弹框
     */
    public static void showWebPushDialog(FragmentActivity activity) {
        Action action = ModuleMgr.getCommonMgr().getCommonConfig().getAction();
        if (action.canPushShow()) {
            WebPushDialog webPushAct = new WebPushDialog(activity, action.getUrl(), action.getDialog_rate());
            webPushAct.showDialog(activity);
        }
    }

    /**
     * 显示订单确认界面
     */
    public static void showRefreshDlg(FragmentActivity activity) {
        Intent intent = new Intent(activity, PayRefreshDlg.class);
        activity.startActivity(intent);
    }

    /**
     * @param stat 支付结果 1支付成功 2支付失败 3订单初始化 -1暂无此订单
     */
    public static void showResultDlg(FragmentActivity activity, int stat) {
        Intent intent = new Intent(activity, PayResultDlg.class);
        intent.putExtra("stat", stat);
        activity.startActivity(intent);
    }

    /**
     * 打开QQ客服
     */
    public static void showQQService(Context context) {
        showQQService(context, "3338566907");
    }

    /**
     * 打开随机QQ客服
     */
    public static void showQQService(Context context, String qq) {
        try {
            String url = "mqqwpa://im/chat?chat_type=wpa&uin=" + qq;
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            if (intent.resolveActivity(context.getPackageManager()) != null) {
                context.startActivity(intent);
            } else {
                PToast.showShort(context.getString(R.string.qq_not_install));
            }
        } catch (Exception e) {
            PToast.showShort(context.getString(R.string.qq_open_error));
        }
    }

    // -----------------------我的提示跳转 start----------------------------

    /**
     * 跳转到我的礼物页面
     */
    public static void showMyGiftActivity(Context context) {
        context.startActivity(new Intent(context, MyGiftActivity.class));
    }

    /**
     * 打开钻石说明页面
     */
    public static void showMyDiamondsExplainAct(Context context) {
        context.startActivity(new Intent(context, MyDiamondsExplainAct.class));
    }

    private static BottomBannedDialog bannedDialog = null;

    /**
     * 账号封禁弹框
     *
     * @param isLogin    是否处于登录状态
     * @param bannedTime 封禁时间戳
     */
    public static void showBottomBannedDlg(final Context context, final boolean isLogin, long bannedTime) {
        bannedDialog = null;
        bannedDialog = new BottomBannedDialog();
        bannedDialog.setCtx(context, isLogin, bannedTime);
        bannedDialog.showDialog((FragmentActivity) context);
    }

    /**
     * 打开提现说明页面
     */
    public static void showWithDrawExplainAct(Context context) {
        context.startActivity(new Intent(context, WithDrawExplainAct.class));
    }

    /**
     * 打开黑名单界面
     */
    public static void showMyDefriends(Context context) {
        Intent intent = new Intent(context, MyDefriendAct.class);
        context.startActivity(intent);
    }

    /**
     * 聊天钻石充值弹框
     */
    public static void showGoodsDiamondBottomDlg(Context context) {
        GoodsDiamondBottomDlg diamondBottomDlg = new GoodsDiamondBottomDlg();
        diamondBottomDlg.showDialog((FragmentActivity) context);
    }
}
